package com.cupk.mapper;

import com.cupk.pojo.Grade;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface GradeMapper {
    @Insert("insert into grade(stu_id, match_id, state, state_name, teacher_id, note, update_time) values (#{grade.stuId},#{grade.matchId},#{grade.state},#{grade.stateName},#{grade.teacherId},#{grade.note},#{nowTime})")
    void addGrade(Grade grade, LocalDateTime nowTime);

    @Update("update grade set state = #{grade.state}, state_name = #{grade.stateName}, teacher_id = #{grade.teacherId}, note = #{grade.note}, update_time = #{nowTime} where match_id = #{grade.matchId} and stu_id = #{grade.stuId}")
    void updateGrade(Grade grade, LocalDateTime nowTime);

    @Select("select * from grade where stu_id = #{stuId} and match_id = #{matchId}")
    Grade findGradeRecordByStuAndMatch(Integer stuId, Integer matchId);

    @Select("select s.name AS stuName, s.stu_id AS stuIdCard, s.grade AS stuGrade, s.college AS stuCollege, s.major AS stuMajor, s.classes AS stuClasses, " + "s.phone AS stuMobile, s.identity_card AS identityCard, m.name AS matchName, m.session AS matchSession, g.state_name AS stateName, g.note as note, " + "u.name AS teacherName, g.update_time AS updateTime FROM grade g LEFT JOIN `user` u ON g.teacher_id = u.id LEFT JOIN student s ON g.stu_id = s.id " + "LEFT JOIN `match` m ON g.match_id = m.id where g.match_id = #{matchId}")
    List<Map<String, Object>> getListByMatchId(Integer matchId);

    @Select("select g.state_name,count(g.id) as record_count from grade g where g.match_id = #{matchId} group by g.state_name")
    List<Map<String, Object>> awardLevelClassification(Integer matchId);

    @Select("select grade.*,`match`.name as matchName,`match`.session as matchSession from grade,`match` where grade.stu_id = #{stuId} and grade.match_id = `match`.id order by grade.update_time desc")
    List<Grade> getAward(Integer stuId);

    @Update("update student set status = #{status} where stu_id = #{stuId}")
    void setBlackListStatus(Integer stuId, Integer status);
}
